<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<% 
String path = request.getScheme()+"://"+request.getServerName()
+":"+request.getServerPort()+request.getContextPath()+"/"; 
// 将 "项目路径basePath" 放入pageContext中，待以后用EL表达式读出。 
pageContext.setAttribute("path",path); 
%>     
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>编辑收货地址</title>
<script src="${path}/resources/js/jquery-1.9.1.min.js"></script>
</head>
<body>
<!-- page-header -->
	<div class="page-header">
		<div class="page-comm">
	 		<div class="mt"> 
	 	 		<h3>编辑收货地址</h3>
	 	 	</div>
	 	 	<div class="mc">
	 	 		<p>设置好轻松购信息，就可以在商品页面直接下单 ，购物从此变得简单快乐。</p>
	 	 	</div>
		</div>	
	</div>
	<!--/.page-header -->
	
	<!-- page-content -->
	<div class="page-content">
		<div class="page-comm">
		
			<!-- 表单开始 form begin -->					
			<form class="form-horizontal" id="validation-form" method="post" action="${path}/manage/address/update/${address.id}?prevurl=${prevurl}">
			
				<input type="hidden" name="id" id="id" value="${address.id}"  >
				<input type="hidden" name="address" id="address" value="${address.address}">
				<input type="hidden" name="status" id="status" value="${address.status}" >
				<input type="hidden" name="member.id" value="${shiroMember.member.id}">
			
				<div class="form-group">
				    <label for="inputEmail3" class="col-md-2 control-label"><em>*</em>收货人：</label>
				    <div class="col-md-4">
			      		<input type="text" class="form-control" id="receiverName"   name="receiverName" value="${address.receiverName}"/>
				    </div>
		 	 	</div>

				<div class="form-group">
				    <label for="inputEmail3" class="col-md-2 control-label"><em>*</em>联系电话：</label>
				    <div class="col-md-4">
			      		<input type="text" class="form-control" id="mobile" name="mobile" value="${address.mobile}"/>
				    </div>
		 	 	</div>
		 	 	
		 	 	<div class="form-group">
				    <label for="inputEmail3" class="col-md-2 control-label"><em>*</em>所在地区：</label>
				    <div class="col-md-4">
			      		<select id="provinceid" name="provinceid" class="form-control"  >
							<option value="-1">请选择省级</option>
							<c:forEach items="${provinceList }" var="province" >
								<c:if test="${province.id==address.provinceid}">
									<option value ="${province.id}" selected )>${province.name}</option>
								</c:if>
								<c:if test="${province.id!=address.provinceid}">
									<option value ="${province.id}"  )>${province.name}</option>
								</c:if>
							</c:forEach>
						</select>
						<br>	
						<select id="cityid" name="cityid" class="form-control" >
							<option value="">请选择市</option>
							<c:forEach items="${cityList }" var="city">
							<c:if test="${city.id==address.cityid }">
								<option value ="${city.id}" selected )>${city.name}</option>
							</c:if>
							<c:if test="${city.id!=address.cityid }">
								<option value ="${city.id}"  )>${city.name}</option>
							</c:if>
							</c:forEach>
						</select>
						<br>	
						<select id="areaid" name="areaid" class="form-control" >
							<option value="">请选择地区</option>
							<c:forEach items="${areaList}" var="area">
								<c:if test="${area.id==address.areaid}">
									<option value ="${area.id}" selected )>${area.name}</option>
								</c:if>
								<c:if test="${area.id!=address.areaid}">
									<option value ="${area.id}"  )>${area.name}</option>
								</c:if>
							</c:forEach>
						</select>
				    </div>
		 	 	</div>
				

 				<div class="form-group">
				    <label for="inputEmail3" class="col-md-2 control-label"><em>*</em>所在街道：</label>
				    <div class="col-md-4">
						<textarea class="form-control"  id="street" name="street" rows="3"  maxlength="100">${address.street}</textarea>
				    </div>
		 	 	</div>
		 	 	
	 	 		<div class="form-group">
				    <label for="inputEmail3" class="col-md-2 control-label"><em>*</em>默认地址：</label>
				    <div class="col-md-4">
						<label class="radio-inline">
						  <input type="radio" name="isDefault" value="1" ${(address.isDefault==1) ? string('checked','')}> 是
						</label>
						<label class="radio-inline">
						  <input type="radio" name="isDefault" value="0"  ${(address.isDefault==0) ? string('checked','')}> 否
						</label>
				    </div>
		 	 	</div>
				
				<div class="form-group">
				    <label for="inputEmail3" class="col-md-2 control-label"></label>
				    <div class="col-md-4">
				    	<a class="btn btn-danger" href="javascript:void(0);" onclick="submitFunc()">提交</a>
				    </div>
		 	 	</div>
			</form>	
		</div>				
	</div>
</body>
<script>

		$(function(){
			//省市区联动
			$("#provinceid").change(function(){
				var pid =$("#provinceid").find("option:selected").val();
				if(pid!=-1){
					$.ajax({
						type:"get",
						url:"${path}/manage/address/ssq/"+pid+"",
						success:function( result ){
							$("#cityid").empty();
							var html = "<option value=\"-1\">请选择</option>";
							if(result.length>0){
								for(var i=0;i<result.length;i++){
									html+= "<option value='"+result[i].id+"'>"+result[i].name+"</option>";
								}
							}
							$("#cityid").append(html);
						}
					})
				}
			})
			
			//省市区联动
			$("#cityid").change(function(){
				var pid =$("#cityid").find("option:selected").val();
				if(pid!=-1){
					$.ajax({
						type:"get",
						url:"${path}/manage/address/ssq/"+pid+"",
						success:function( result ){
							$("#areaid").empty();
							var html = "<option value=\"-1\">请选择</option>";
							if(result.length>0){
								for(var i=0;i<result.length;i++){
									html+= "<option value='"+result[i].id+"'>"+result[i].name+"</option>";
								}
							}
							$("#areaid").append(html);
						}
					})
				}
			})
		})


		function submitFunc(){
			var province = $("#provinceid").find("option:selected").text();
			var provinceid = $("#provinceid").find("option:selected").val();
			var city = $("#cityid").find("option:selected").text();
			var cityid = $("#cityid").find("option:selected").val();
			var area = $("#areaid").find("option:selected").text();
			var areaid = $("#areaid").find("option:selected").val();
			var street = $("#street").val();
			street = $.trim(street)
			if(street==null || street=="" || provinceid==-1 || cityid==-1 ){
				alertSuccess("请完善您的收货地址");
				return false; 
			}
			
			var mobile = /^(1[0-9])\d{9}$/; 
			if(!mobile.test($("#mobile").val())){
				alertSuccess("请输入正确的手机号");
				return false;
			}
			
			var address="";
			if(areaid!=-1){
				address+=province+city+area+street;
			}else{
				address+=province+city+street;
			}
			$("#address").val(address);
			var isDefault = $("input[name='isDefault']:checked").val();
			var flag = false;
			if(isDefault==1){
				flag = confirm("是否将此地址设置成为默认地址 ?");
				if(flag){
					$("#validation-form").submit();
				}
			}else{
				$("#validation-form").submit();
			}
		}
	</script>
</html>